package com.songlanyun.modules.logisticsconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.enums.OrderConstant;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.common.validator.group.AddGroup;
import com.songlanyun.common.validator.group.UpdateGroup;
import com.songlanyun.modules.logisticsconfig.dao.LogisticsConfigDao;
import com.songlanyun.modules.logisticsconfig.entity.LogisticsConfig;
import com.songlanyun.modules.logisticsconfig.model.dto.LogisticsConfigDTO;
import com.songlanyun.modules.logisticsconfig.service.LogisticsConfigService;
import com.songlanyun.modules.shop.service.ShopService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service("logisticsConfigService")
public class LogisticsConfigServiceImpl extends ServiceImpl<LogisticsConfigDao, LogisticsConfig> implements LogisticsConfigService {
    @Autowired
    private ShopService shopService;

    /**
     * 创建物流配置（内部调用）
     * @param entity
     * @return
     */
    public LogisticsConfig create(LogisticsConfig entity) {
        ValidatorUtils.validateEntity(entity, AddGroup.class);
        this.baseMapper.insert(entity);
        return entity;
    }

    /**
     * 修改物流配置（内部调用）
     * @param entity
     * @return
     */
    public LogisticsConfig modify(LogisticsConfig entity) {
        ValidatorUtils.validateEntity(entity, UpdateGroup.class);
        this.baseMapper.updateById(entity);
        return entity;
    }

    /**
     * 商家设置自己店铺的物流配置
     * @param shopId
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public LogisticsConfig set(Long shopId, LogisticsConfigDTO dto) {
        shopService.loadShopBy(shopId,true);
        LogisticsConfig config = this.getLogisticsConfig(shopId);
        if(config==null)
        {
            LogisticsConfig logisticsConfig = new LogisticsConfig();
            BeanUtils.copyProperties(dto,logisticsConfig);
            logisticsConfig.setExpiredOrderHandleType(OrderConstant.ExpiredOrderHandleMode.getByCode(dto.getExpiredOrderHandleType()));
            logisticsConfig.setFreightStrategy(OrderConstant.FreightStrategy.getByCode(dto.getFreightStrategy()));
            logisticsConfig.setShopId(shopId);
            this.create(logisticsConfig);
            return logisticsConfig;
        }
        else
        {
            Long id = config.getId();
            BeanUtils.copyProperties(dto,config);
            config.setExpiredOrderHandleType(OrderConstant.ExpiredOrderHandleMode.getByCode(dto.getExpiredOrderHandleType()));
            config.setFreightStrategy(OrderConstant.FreightStrategy.getByCode(dto.getFreightStrategy()));
            config.setId(id);
            this.modify(config);
            return config;
        }
    }

    /**
     * 获取商家的物流配置
     * @param shopId
     * @return
     */
    @Override
    public LogisticsConfig getLogisticsConfig(Long shopId) {
        return this.baseMapper.selectOne(
                new LambdaQueryWrapper<LogisticsConfig>().
                        eq(LogisticsConfig::getShopId,shopId).last("limit 1")
        );
    }





}